﻿Public Class currency
    Public Function dec2curr(ByVal value As Decimal) As String
        Return value.ToString("N2", System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))
    End Function

    Private Function TValue(ByVal LValue As Integer) As String
        Dim value As String = " "
        Select Case LValue
            Case 1
                value = "Thousand "
            Case 2
                value = "Million "
            Case 3
                value = "Billion "
            Case 4
                value = "Trillion "
        End Select
        Return value
    End Function

    Public Function NumberToString(ByVal num As Double) As String
        Dim str As Array = dec2curr(num).ToString().Split(".")
        If num = 0 Then Return "zero"
        num = Int(num)

        Static groups() As String = {"", "thousand", "million", _
            "billion", "trillion", "quadrillion", "?", "??", _
            "???", "????"}
        Dim result As String = ""

        Dim quotient As Double
        Dim remainder As Integer
        Dim group_num As Integer = 0
        Do While num > 0
            quotient = Int(num / 1000)
            remainder = CInt(num - quotient * 1000)
            num = quotient

            If (remainder > 0) Then
                If (result <> "") Then
                    result = ", " & result
                End If
                result = GroupToWords(remainder) & " " & groups(group_num) & result
            End If

            group_num += 1
        Loop
        If result.EndsWith(", ") Then
            result = result.Substring(0, result.Length - 4)
        End If
        If (str.Length > 1) Then
            Dim dec As Integer = Convert.ToInt32(str(1).ToString)
            If (dec > 0) Then
                If (result <> "") Then
                    result += " and "
                Else : result += " "
                End If
                result += GroupToWords(dec) & " Cent"
            End If
        End If

        Return result.Trim()
    End Function

    Private Function GroupToWords(ByVal num As Integer) As _
        String
        Static one_to_nineteen() As String = {"zero", "one", _
            "two", "three", "four", "five", "six", "seven", _
            "eight", "nine", "ten", "eleven", "twelve", _
            "thirteen", "fourteen", "fifteen", "sixteen", _
            "seventeen", "eightteen", "nineteen"}
        Static multiples_of_ten() As String = {"twenty", _
            "thirty", "forty", "fifty", "sixty", "seventy", _
            "eighty", "ninety"}

        If num = 0 Then Return ""

        Dim digit As Integer
        Dim result As String = ""
        If num > 99 Then
            digit = num \ 100
            num = num Mod 100
            result = one_to_nineteen(digit) & " hundred"
        End If

        If num = 0 Then Return result.Trim()

        If num < 20 Then
            result &= " " & one_to_nineteen(num)
        Else
            digit = num \ 10
            num = num Mod 10
            result &= " " & multiples_of_ten(digit - 2)

            If num > 0 Then
                result &= " " & one_to_nineteen(num)
            End If
        End If

        Return result.Trim()
    End Function
End Class
